import QBar from "../Com/QBar";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Role extends cc.Component {
    protected animation:cc.Animation;
    /** 初始位置 */
    protected originPosition:cc.Vec2;    
    /** 攻击目标 */
    protected target:Role;
    /** 阵营 player enemey */
    camp:string;
    /** 减血提示 */
    protected subHpTip:cc.Label
    /** 血条 */
    protected hpBar:QBar;
    /** 蓝条 */
    protected mpBar:QBar;
    /** 基础数据 */
    protected baseData:QM.Player|Db.Enemy
    onLoad(){
        this.animation = this.getComponent(cc.Animation);
        this.subHpTip = this.node.getChildByName("SubHpTip").getComponent(cc.Label);
        this.node.on(cc.Node.EventType.TOUCH_END,this.ClickRole,this);
        var hpNode = this.node.getChildByName("hpBar");
        if(hpNode) this.hpBar = hpNode.getComponent(QBar);
        var mpNode = this.node.getChildByName("mpBar");
        if(mpNode) this.mpBar = mpNode.getComponent(QBar);
        
        
    }
       
    start(){
        this.originPosition = this.node.position;
        if(this.hpBar){
            this.hpBar.init(10000,10000);
        }
        this.Qstart();
    }   
   
    Qstart(){}

    /** 角色被点击时 */
    ClickRole(){
        var event = new cc.Event.EventCustom("ClickRole",true);
        this.node.dispatchEvent(event);
    }

    /** 攻击目标 */
    attack(target:Role){}

    /** 受到攻击时的动作 */
    Beaten(){}

    /** 受到伤害攻击 提示减血 */
    SubHpTip(hp:number){
        hp = RandomInt(0,999);
        this.subHpTip.string = "-"+hp;
        this.subHpTip.node.opacity = 0;

        var a1 = cc.fadeIn(0.05);
        var a2 = cc.moveBy(0.4,0,80);
        var a3 = cc.fadeOut(0.2);
        var a4 = cc.moveTo(0,cc.Vec2.ZERO);
        this.subHpTip.node.runAction(cc.sequence(a1,a2,a3,a4));
        if(this.hpBar){
            this.hpBar.change(-hp);
        }

    }


    /** 角色操作结束 */
    RoleEnd(){        
        this.showBar();
        var event = new cc.Event.EventCustom("RoleEnd",true);
        this.node.dispatchEvent(event);
    }

    hideBar(){
        if(this.hpBar) this.hpBar.node.active = false;
        if(this.mpBar) this.mpBar.node.active = false;
        
    }
    showBar(){
        if(this.hpBar) this.hpBar.node.active = true;
        if(this.mpBar) this.mpBar.node.active = true;
    }
    
}
